package net.mgsx.product.t2m.modisco.utility;
import org.eclipse.gmt.modisco.infra.query.core.exception.ModelQueryExecutionException;
import org.eclipse.gmt.modisco.infra.query.core.java.IJavaModelQuery;
import org.eclipse.gmt.modisco.infra.query.core.java.ParameterValueList;
import org.eclipse.gmt.modisco.java.BodyDeclaration;
import org.eclipse.gmt.modisco.java.ClassDeclaration;
import org.eclipse.gmt.modisco.java.MethodDeclaration;

public class IsSingleton implements IJavaModelQuery<ClassDeclaration, Boolean> {
	public Boolean evaluate(final ClassDeclaration context, final ParameterValueList parameterValues)
			throws ModelQueryExecutionException {
		
		// a class is a singleton if there is at least one
		// public static method without parameters returning an 
		// instance of its type.
		
		boolean result = false;
		
		for(BodyDeclaration declaration : context.getBodyDeclarations())
		{
			if(declaration instanceof MethodDeclaration)
			{
				MethodDeclaration md = (MethodDeclaration)declaration;
				if(md.getModifier() != null && md.getModifier().isStatic())
				{
					if(md.getParameters().size() == 0)
					{
						if(md.getReturnType() != null && md.getReturnType().getType() == context)
						{
							result = true;
							break;
						}
					}
				}
			}
		}
		
		return result;
	}
}
